package org.example.service.impl;

import org.example.mapper.ScheduleTaskMapper;
import org.example.scheduledEntity.ScheduSpringCron;
import org.example.service.ScheduTaskService;
import org.example.util.R;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.xml.bind.ValidationException;
import java.util.Date;
import java.util.List;

/**
 * @author 张在成
 */
@Service
public class ScheduTaskServiceImpl implements ScheduTaskService {

    @Resource
    private ScheduleTaskMapper  taskMapper;

    /**
     * 获取全部定时任务列表
     * @return
     */
    @Override
    public R<List<ScheduSpringCron>> getScheduleTaskList() {
        List<ScheduSpringCron> scheduleTaskList = taskMapper.getScheduleTaskList();
        if (scheduleTaskList==null){
            R.data(400,"数据库无该数据",scheduleTaskList);
        }
        return R.data(200,"成功",scheduleTaskList);
    }

    /**
     * 新增定时任务
     * @param springCron
     * @return
     */
    @Override
    public boolean insertScheduleTask(ScheduSpringCron springCron) throws Exception {
        if (springCron==null){
            throw new Exception("参数为空");
        }
        if (springCron.getCronKey()==null||springCron.getCronExpression()==null){
            throw  new Exception("类的全限定名或者cron表达式不能为null");
        }
        springCron.setCreateTime(new Date());
        springCron.setStatus(1);
        boolean scheduleTask= taskMapper.insertScheduleTask(springCron);
        if (!scheduleTask){
            throw new  Exception("添加任务失败");
        }
        return  scheduleTask;
    }

    /**
     * 批量新增定时任务
     * @param springCron
     * @return
     */
    @Override
    public int insertBatchScheduleTask(List<ScheduSpringCron> springCron) {

        System.out.println("批量添加定时任务");

        return 0;
    }

    /**
     * 批量修改修改定时任务
     * @param springCron
     * @return
     */
    @Override
    public int updateBatchScheduleTask(List<ScheduSpringCron> springCron) {
        return 0;
    }

    /**
     *  修改定时任务信息
     * @param springCron
     * @return
     */
    @Override
    public boolean updateScheduleTask(ScheduSpringCron springCron) throws ValidationException {
        if (springCron.getCronId()==null||springCron.getStatus()==null){
            throw new ValidationException("参数校验为空");
        }
        boolean task = taskMapper.updateScheduleTask(springCron);

        if (!task){
            throw new ValidationException("定时任务修改失败");
        }
        return task;
    }

    /**
     * 查询单个定时任务
     * @param cronId
     * @return
     */
    @Override
    public ScheduSpringCron getScheduleTaskByCronId(Integer cronId) {
        return null;
    }


    /**
     *  删除定时任务
     * @param cronId
     * @return
     */
    @Override
    public int delScheduleTask(Integer cronId) {
        return 0;
    }
}
